﻿using System.Collections.Generic;
using System.Reflection;
using Terraria;
using Terraria.Achievements;
using Terraria.ModLoader;

namespace DamnAchievements
{
    public class DamnAchievements : Mod
    {
    }
    /// <summary>
    /// 通过反射来获取条件从而强制完成成就
    /// </summary>
    public class Worlds : ModPlayer
    {
        public override void OnEnterWorld()
        {
            CompleteAllAchievements();
        }
        private static void CompleteAllAchievements()
        {
            var achievementsList = Main.Achievements.CreateAchievementsList();
            foreach (Achievement achievement in achievementsList)
            {
                CompleteAchievementConditions(achievement);
            }
        }
        private static void CompleteAchievementConditions(Achievement achievement)
        {
            var conditionsField = typeof(Achievement).GetField("_conditions", BindingFlags.Instance | BindingFlags.NonPublic);
            if (conditionsField != null)
            {
                var conditions = (Dictionary<string, AchievementCondition>)conditionsField.GetValue(achievement);
                if (conditions != null)
                {
                    foreach (var cond in conditions.Values)
                    {
                        cond?.Complete();
                    }
                    achievement.UseConditionsCompletedTracker();
                }
            }
        }
    }
}